﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
namespace WPFExcelReport
{ 
    public partial class VVRKOPF
    {
        public void CHECKBYEKK(Dictionary<string, object> pm)
        {
            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            SResult = EKK.EKKLOAD(PM);
            TBEKK = SResult.Tables[0].Copy();

            PM["RENR"] = pm["RENR"].ToString();
            SResult = LOAD(PM);
            TBVRK = SResult.Tables[0].Copy();
            foreach (DataRow row in TBEKK.Rows)
            {
                foreach (DataRow dwVRK in TBVRK.Rows)
                {
                    dwVRK["VRKZAHLBED"] = row["EKKZAHLBED"];
                    // dwVRK["VRKLIEFNR"] = row["EKKLIEFNR"];
                    UpdateVRKOPF(dwVRK, app.company, 2);
                }
            }

            PM["RENR"] = pm["RENR"].ToString();
            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            PM["AUFTYP"] = pm["AUFTYP"];
            SResult = EKP.LOADBYEK(PM);
            TBEKP = SResult.Tables[0].Copy();

            PM["RENR"] = pm["RENR"].ToString();
            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            PM["AUFTYP"] = pm["AUFTYP"];
            VRP.CLEANBYVKK(PM);

            PM["RENR"] = pm["RENR"].ToString();
            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            PM["AUFTYP"] = pm["AUFTYP"];
            SResult = VRP.LOADBYVKK(PM);
            TBVRP = SResult.Tables[0].Copy();

            PM["KRS"] = pm["KRS"].ToString();
            PM["LFD"] = pm["LFD"].ToString();
            PM["AUFTYP"] = pm["AUFTYP"];
            PM["VKZSA"] = "EK";
            SResult = VZB.LOADBYVKK(PM);
            TBVKZ = SResult.Tables[0].Copy();

            string ZOLLTNR = ("0:100").PadRight(20);
            foreach (DataRow dwekp in TBEKP.Rows)
            {

                filter = "VRPRENR='" + PM["RENR"].ToString() + "' AND VRPAUFKRS='" + dwekp["EKPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwekp["EKPAUFLFD"].ToString() + "' AND VRPAUFPOS='" + dwekp["EKPAUFPOS"].ToString() + "' AND VRPZOLLTNR='" + "0:100".PadRight(20) + "' ";
                sort = " VRPREPOS ASC ";
                rows = TBVRP.Select(filter, sort);
                if (rows.Length > 0) continue;
                dwZB = null;
                filter = "VKZAUFKRS='" + dwekp["EKPAUFKRS"].ToString() + "' AND VKZAUFLFD='" + dwekp["EKPAUFLFD"].ToString() + "' AND VKZAUFPOS='" + dwekp["EKPAUFPOS"].ToString() + "'";
                rows = TBVKZ.Select(filter);
                foreach (DataRow row in rows)
                {
                    dwZB = row;
                }
                if (dwZB == null) continue;
                VRP.NEWBYEKP(dwekp, dwZB, PM["RENR"].ToString());
            }
        }

        #region======EK=======
        public void VVT2EKVR(Dictionary<string, object> pm, string RENR, int paybal)
        {
            try
            {
                if (paybal < 2) return;
                PM["AUFTYP"] = pm["AUFTYP"];
                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["POS"] = pm["POS"];
                PM["WERT"] = pm["WERT"];
                PM["DELDATE"] = pm["DELDATE"];
                PM["ROW"] = pm["ROW"];
                PM["QTY"] = pm["QTY"];
                PM["LDDATE"] = pm["LDDATE"];
                SplitByPT(PM, RENR, paybal);//产生提货款发票
                if (TBVVT == null) return;
                ratio = 0.01 * 0.01 * Convert.ToDouble(dwZA["ZABZBIN" + paybal].ToString());
                if (ratio == 0)
                    return;

                double VVTWERT = 0;
                DataRow dwVRP = null;
                DataRow dwVRMom = null;
                bool IsVVPayExists = false;

                double qty = VVEKQTY(PM, RENR);
                VVTWERT = EKP.CalculateGoodsValue((DataRow)PM["ROW"], qty);
                if (paybal == 2)
                {
                    PM["QTY"] = qty; 
                    dwVRMom = ResetBaseVR(PM);//根据实际数量，重新计算价值
                }

                filter = "VRPAUFKRS='" + pm["KRS"].ToString() + "' AND VRPAUFLFD='" + pm["LFD"].ToString() + "' AND VRPAUFPOS='" + pm["POS"].ToString()
                + "' AND VRPRENR='" + RENR + "' AND VRPZOLLTNR LIKE '2:%'";
                sort = " VRPREPOS ASC ";

                rows = TBVRP.Select(filter, sort);
                if (rows.Length > 0)
                {
                    dwVRP = rows[0];//需要中款
                    IsVVPayExists = true;
                }
                if (dwVRP == null)
                    return;
                if (dwVRP["VRPERLKZ"].ToString() == "J")//已经支付的不再处理
                    return;
                if (VVTWERT == 0) return;
                if (IsVVPayExists)
                {
                    dwVRMom["VRPWAWERT"] = Helper.Right("0000000000" + (VVTWERT * 1000).ToString(), 10);//货物的总价值更新为
                    dwVRP["VRPWAWERT"] = Helper.Right("0000000000" + (VVTWERT * 1000).ToString(), 10);//附加费考虑后的值 ;//净值
                    double value = Helpers.Helper.RoundX(VVTWERT * ratio, 1);
                    if (value < 0)
                        value = 0;
                    value += VRP.GETABADD(dwVRMom);
                    dwVRP["VRPGESWERT"] = Helper.Right("0000000000" + (value * 1000).ToString(), 10);//附加费考虑后的值
                }

                VRP.UpdateVRPOS(dwVRP, 0);
                RESTMOM(PM, dwVRMom);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public double VVEKQTY(Dictionary<string, object> pm, string RENR)
        {
            double qty = 0;
            string auftyp = pm["AUFTYP"].ToString();

            filter = "EKPAUFKRS='" + pm["KRS"].ToString() + "' AND EKPAUFLFD='" + pm["LFD"].ToString() + "' AND EKPAUFPOS='" + pm["POS"].ToString() + "'";
            rows = TBEKP.Select(filter);
            foreach (DataRow row in rows)
            {
                qty = 0.01 * Convert.ToDouble(row["EKPME3"].ToString());
            }
            if (qty == 0)
            {
                qty = (double)pm["QTY"];
            }
            return qty;
        }
        public void VVT2EKREST(Dictionary<string, object> pm, string RENR, int paybal)
        {
            try
            {
                if (paybal < 2) return;
                PM["AUFTYP"] = pm["AUFTYP"];
                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["POS"] = pm["POS"];
                PM["WERT"] = pm["WERT"];
                PM["DELDATE"] = pm["DELDATE"];
                PM["ROW"] = pm["ROW"];
                PM["QTY"] = pm["QTY"];
                PM["LDDATE"] = pm["LDDATE"];
                filter = "VRPAUFKRS='" + pm["KRS"].ToString() + "' AND VRPAUFLFD='" + pm["LFD"].ToString() + "' AND VRPAUFPOS='" + pm["POS"].ToString()
                + "' AND VRPRENR='" + RENR + "' AND VRPERLKZ=' ' AND (VRPZOLLTNR LIKE '2:%' OR VRPZOLLTNR LIKE '1:%')";
                sort = " VRPREPOS ASC ";
                rows = TBVRP.Select(filter);
                if (rows.Length > 0)
                {
                    return;
                }
                SplitByPT(PM, RENR, paybal);//产生尾款发票 
                #region=====3=

                DataRow dwVRP = null;
                filter = "VRPAUFKRS='" + pm["KRS"].ToString() + "' AND VRPAUFLFD='" + pm["LFD"].ToString() + "' AND VRPAUFPOS='" + pm["POS"].ToString()
                + "' AND VRPRENR='" + RENR + "' AND VRPZOLLTNR LIKE '3:%'";
                sort = " VRPREPOS ASC ";
                rows = TBVRP.Select(filter);
                if (rows.Length == 0)
                {
                    return;
                }
                dwVRP = rows[0];//需要尾款
                if (dwVRP["VRPERLKZ"].ToString() == "J")//已经支付的不再处理
                    return;
                #endregion
                DataRow dwVRMom = null;
                filter = "VRPAUFKRS='" + pm["KRS"].ToString() + "' AND VRPAUFLFD='" + pm["LFD"].ToString() + "' AND VRPAUFPOS='" + pm["POS"].ToString() + "' AND VRPZOLLTNR='" + "0:100".PadRight(20) + "' ";
                sort = " VRPREPOS ASC ";
                rows = TBVRP.Select(filter, sort);
                if (rows.Length == 0)
                {
                    return;
                }

                dwVRMom = rows[0];
                double qty = VVEKQTY(PM, RENR);
                double VVTWERT = EKP.CalculateGoodsValue((DataRow)PM["ROW"], qty);//再次根据发货量，更新价值
                dwVRMom["VRPWAWERT"] = Helper.Right("0000000000" + (VVTWERT * 1000).ToString(), 10);//货物的总价值更新为
                PM["QTY"] = qty;
                RESTMOM(PM, dwVRMom);
                dwVRP["VRPGESWERT"] = dwVRMom["VRPGESWERT"];//货物的总价值更新为 
                dwVRMom["VRPGESWERT"] = 0;//附加费考虑后的值 ;//净值
                VRP.UpdateVRPOS(dwVRP, 0);
                VRP.UpdateVRPOS(dwVRMom, 0);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void VV2EKREST(Dictionary<string, object> pm)
        {

            PM["KRS"] = pm["KRS"];
            PM["LFD"] = pm["LFD"];
            PM["RENR"] = pm["RENR"];
            PM["ZAHLBED"] = pm["ZAHLBED"];
            PM["PAYPAL"] = pm["PAYPAL"];
            string RENR = pm["RENR"].ToString();
            string order = pm["ORDERNUM"].ToString();
            string _ZAHLBED = pm["ZAHLBED"].ToString();
            int paypal = (int)pm["PAYPAL"];
            PrepareEK(PM, false);
            if (TBZA == null)
                return;
            if (TBZA.Rows.Count == 0)
                return;
            dwZA = TBZA.Rows[0];
            int ZABANZAHLG = Convert.ToInt16(dwZA["ZABANZAHLG"].ToString());
            foreach (DataRow dwEKP in TBEKP.Rows)
            {
                PM["AUFTYP"] = pm["AUFTYP"];
                PM["KRS"] = dwEKP["EKPAUFKRS"].ToString();
                PM["LFD"] = dwEKP["EKPAUFLFD"].ToString();
                PM["POS"] = dwEKP["EKPAUFPOS"].ToString();
                PM["WERT"] = dwEKP["EKPWE1"].ToString();
                PM["DELDATE"] = dwEKP["EKPLTERM"].ToString();
                PM["ROW"] = dwEKP;
                PM["QTY"] = 0.01 * Convert.ToDouble(dwEKP["EKPME1"].ToString());
                PM["LDDATE"] = ZABANZAHLG;
                VVT2EKREST(PM, RENR, paypal);
            }
        }
        public void VVEKVR(Dictionary<string, object> pm)
        {
            PM["KRS"] = pm["KRS"];
            PM["LFD"] = pm["LFD"];
            PM["RENR"] = pm["RENR"];
            PM["ZAHLBED"] = pm["ZAHLBED"];
            PM["PAYPAL"] = pm["PAYPAL"];
            string RENR = pm["RENR"].ToString();
            string order = pm["ORDERNUM"].ToString();
            string _ZAHLBED = pm["ZAHLBED"].ToString();
            int paypal = (int)pm["PAYPAL"];
            PrepareEK(PM, false);
            if (TBZA == null)
                return;
            if (TBZA.Rows.Count == 0)
                return;
            dwZA = TBZA.Rows[0];
            int ZABANZAHLG = Convert.ToInt16(dwZA["ZABANZAHLG"].ToString());
            foreach (DataRow dwEKP in TBEKP.Rows)
            {
                PM["AUFTYP"] = pm["AUFTYP"];
                PM["KRS"] = dwEKP["EKPAUFKRS"].ToString();
                PM["LFD"] = dwEKP["EKPAUFLFD"].ToString();
                PM["POS"] = dwEKP["EKPAUFPOS"].ToString();
                PM["WERT"] = dwEKP["EKPWE1"].ToString();
                PM["DELDATE"] = dwEKP["EKPLTERM"].ToString();
                PM["ROW"] = dwEKP;
                PM["QTY"] = 0.01 * Convert.ToDouble(dwEKP["EKPME1"].ToString());
                PM["LDDATE"] = ZABANZAHLG;
                VVT2EKVR(PM, RENR, paypal);
            }
        }
        /// <summary>
        /// paypal 代表首付款，中款还是尾款
        /// </summary>
        /// <param name="paypal"></param>
        /// 
        public void ZHLBEDCHGEK(DataRow dwVRK)
        {
            PM["KRS"] = dwVRK["VRPAUFKRS"].ToString();
            PM["LFD"] = dwVRK["VRPAUFLFD"].ToString();
            SResult = EKK.EKKLOAD(PM);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwVRK["VRKMANDANT"] = app.Mandant;
                dwVRK["VRKZAHLBED"] = row["EKKZAHLBED"].ToString().PadRight(3);
                UpdateVRKOPF(dwVRK, app.company, 2);
            }
        }
        public void PrepareEK(Dictionary<string, object> pm, bool IsReset = false)
        {
            try
            {

                if (IsReset) IsPrepared = false;
                if (IsPrepared) return;

                PM["ZABNR"] = pm["ZAHLBED"]; ;
                SResult = ZA.GETZABED(PM);
                TBZA = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                SResult = EKP.LOADBYEK(PM);
                TBEKP = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["AUFTYP"] = "B";
                SResult = VRP.LOADBYVKK(PM);
                TBVRP = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["AUFTYP"] = "B";
                SResult = VVT.VVLOAD(PM);
                TBVVT = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["AUFTYP"] = "B";
                SResult = VV.VVLOAD(PM);
                TBVV = SResult.Tables[0].Copy();

                PM["KRS"] = pm["KRS"];
                PM["LFD"] = pm["LFD"];
                PM["AUFTYP"] = "B";
                PM["VKZSA"] = "EK";
                SResult = VZB.LOADBYVKK(PM);
                TBVKZ = SResult.Tables[0].Copy();
                IsPrepared = true;
            }
            catch (Exception ex)
            {
                IsPrepared = false;
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public void EKKPT(Dictionary<string, object> pm)
        {
            PM["KRS"] = pm["KRS"];
            PM["LFD"] = pm["LFD"];
            PM["RENR"] = pm["RENR"];
            PM["ZAHLBED"] = pm["ZAHLBED"];
            PM["PAYPAL"] = pm["PAYPAL"];
            string RENR = pm["RENR"].ToString();
            string order = pm["ORDERNUM"].ToString();
            string _ZAHLBED = pm["ZAHLBED"].ToString();
            int paypal = (int)pm["PAYPAL"];
            PrepareEK(PM, false);
            if (TBZA == null)
                return;
            if (TBZA.Rows.Count == 0)
                return;
            dwZA = TBZA.Rows[0];
            int ZABANZAHLG = Convert.ToInt16(dwZA["ZABANZAHLG"].ToString());

            foreach (DataRow dwEKP in TBEKP.Rows)
            {
                PM["KRS"] = dwEKP["EKPAUFKRS"].ToString();
                PM["LFD"] = dwEKP["EKPAUFLFD"].ToString();
                PM["POS"] = dwEKP["EKPAUFPOS"].ToString();
                PM["WERT"] = dwEKP["EKPWE1"].ToString();
                PM["DELDATE"] = dwEKP["EKPLTERM"].ToString();
                PM["ROW"] = dwEKP;
                PM["RENR"] = pm["RENR"];
                PM["QTY"] = 0.01 * Convert.ToDouble(dwEKP["EKPME1"].ToString());
                PM["LDDATE"] = ZABANZAHLG;
                SplitByPT(PM, RENR, paypal);
            }
            IsPrepared = false;
        }
        #endregion
    }
  
}
